<template>
    <div>
        <el-card class="box-card" style="width: 400px">
            <div slot="header" class="clearfix">
                <span>{{admin.name}}</span>
            </div>
            <div>
                <!--头像-->
                <div style="display: flex;justify-content: center">
                    <img title="点击修改用户头像" style="height: 100px; width: 100px; border-radius: 50px"
                         :src="admin.userFace"/>
                </div>
                <div>电话号码:
                    <el-tag>{{admin.telephone}}</el-tag>
                </div>
                <div>手机号码:
                    <el-tag>{{admin.phone}}</el-tag>
                </div>
                <div>居住地址:
                    <el-tag>{{admin.address}}</el-tag>
                </div>
                <div>用户标签:
                    <el-tag type="success" v-for="(r,index) in admin.roles" :key=index>{{r.nameZh}}</el-tag>
                </div>
                <div style="display: flex;justify-content: space-around;margin-top: 10px">
                    <el-button type="primary" @click="showUpdateAdminInfoView">修改信息</el-button>
                    <el-button type="danger" @click="showUpdatePasswordView">修改密码</el-button>
                </div>
            </div>
        </el-card>
        <!--编辑用户信息对话框-->
        <el-dialog
                title="编辑用户信息"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <td>用户昵称:</td>
                        <td>
                            <el-input v-model="adminUpdate.name"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>用户电话号码:</td>
                        <td>
                            <el-input v-model="adminUpdate.telephone"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>用户手机号码:</td>
                        <td>
                            <el-input v-model="adminUpdate.phone"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>居住地址:</td>
                        <td>
                            <el-input v-model="adminUpdate.address"></el-input>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateAdminInfo">确 定</el-button>
            </span>
        </el-dialog>
        <!--修改密码对话框-->
        <el-dialog
                title="更新密码"
                :visible.sync="passwordDialogVisible"
                width="30%">
            <div>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                         class="demo-ruleForm">
                    <el-form-item label="请输入旧密码" prop="oldPass">
                        <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="请输入新密码" prop="pass">
                        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认新密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "UserCenter",
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            }
            return {
                admin: {},
                adminUpdate: {
                    name: '',
                    telephone: '',
                    phone: '',
                    address: '',
                },
                dialogVisible: false,
                passwordDialogVisible: false,
                ruleForm: {
                    //旧密码
                    oldPass: '',
                    pass: '',
                    checkPass: '',
                },
                rules: {
                    pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ],
                    oldPass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                }
            };
        },
        methods: {
            /**
             * 初始化获取用户信息
             */
            initAdmin() {
                this.getRequest('/admin/info').then(res => {
                    if (res) {
                        this.admin = res;
                        //对象拷贝，防止更改弹出框的数据时，基本数据也发生变化
                        this.adminUpdate = Object.assign({}, this.admin);
                        window.sessionStorage.setItem('user', JSON.stringify(res));
                        this.$store.commit('INIT_ADMIN', res);
                    }
                })
            },
            /**
             * 展示修改信息对话框
             */
            showUpdateAdminInfoView() {
                this.dialogVisible = true;
            },
            /**
             * 更新用户基本信息
             */
            updateAdminInfo() {
                this.putRequest('/system/admin/updateAdminInfo', this.adminUpdate).then(res => {
                    if (res) {
                        this.dialogVisible = false;
                        this.initAdmin();
                    }
                })
            },
            /**
             * 展示更新密码对话框
             */
            showUpdatePasswordView() {
                this.passwordDialogVisible = true;
            },
            /**
             * 提交表单
             * @param formName
             */
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.ruleForm.adminId=this.admin.id;
                        this.putRequest('/system/admin/updatePassword', this.ruleForm).then(res => {
                            if (res) {
                                //更新密码成功后退出登录
                                this.postRequest('/logout');
                                //移除sessionStorage中的数据
                                window.sessionStorage.removeItem('user');
                                window.sessionStorage.removeItem('tokenStr');
                                //将菜单置为空
                                this.$store.commit('initRoutes', []);
                                this.$router.replace('/');
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            /**
             * 重置表单
             * @param formName
             */
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        mounted() {
            this.initAdmin();
        }
    }
</script>

<style scoped>

</style>
